import React from 'react';
import {
  View,
  Text,
  Button,
  ScrollView,
  TouchableOpacity,
  Platform,
  CameraRoll,
} from 'react-native';

import BasePage from '../common/BasePage';
import SizeUtils from '../../common/SizeUtils';
import Config from '../../config';
import TImage from '../../components/TImage';
import ImageUtil from '../common/ImageUtil';

export default class GuahaoErweimaPage extends BasePage {
  constructor(props) {
    super(props);

  }

  render() {
    return(
      <View style={{width:SizeUtils.getWindowSize().width, height:SizeUtils.getWindowSize().height,
         paddingTop: Config.paddingTop + SizeUtils.translateDesignWidthToCode(40), alignItems: 'center', backgroundColor:"#ffffff"}}>
        {this._renderNavigationBar()}
        <View style={{backgroundColor:"#ffffff", flexDirection:"column",
            justifyContent:"space-between", alignItems:"center",
            paddingLeft:SizeUtils.translateDesignWidthToCode(40), paddingRight:SizeUtils.translateDesignWidthToCode(40)}}>
          <TouchableOpacity onPress={() => this._saveImage()} style={{paddingTop:SizeUtils.translateDesignHeightToCode(60)}}>
            <TImage source={{uri:this.props.pageData.routeData.buttonData.erweima}}
              style={{width:430, height:430}}/>
          </TouchableOpacity>
          <Text style={{fontSize:SizeUtils.translateFontSizeToCode(30), color:"#666666"}}>
            说明
          </Text>
          <Text style={{fontSize:SizeUtils.translateFontSizeToCode(26), color:"#999999",
              paddingTop:SizeUtils.translateDesignHeightToCode(20)}}>
            点击二维码，将图片保存到相册
          </Text>
          <Text style={{fontSize:SizeUtils.translateFontSizeToCode(26), color:"#999999",
              paddingTop:SizeUtils.translateDesignHeightToCode(20)}}>
            {"点开微信→点击右上角“+”→扫一扫→点击“相册”/“”，选择相册中的二维码→关注医院→导诊→预约挂号"}
          </Text>
        </View>
      </View>
    )
  }

  _saveImage() {
    if(Platform.OS === "android"){
      let data = this.props.pageData.routeData.buttonData.erweima;
      if( this.props.pageData.routeData.buttonData.erweima.indexOf("data:") == 0){
            data = this.props.pageData.routeData.buttonData.erweima.split(",")[1];
      }
      ImageUtil.saveImage(data).then((ok) => {
        alert('保存成功!')
      }).catch((error) => {
        alert('保存失败！\n' + error);
        console.log(error)
      })
      return;
    }
    let promise = CameraRoll.saveToCameraRoll(this.props.pageData.routeData.buttonData.erweima, "photo");
    promise.then(function(result) {
        alert('保存成功!');
      }).catch(function(error) {
        alert('保存失败！\n' + error);
      });
    console.log(promise);
  }

}
